package com.example.dbpractise1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.model.Person;
import com.example.service.PersonService;

public class PersonActivity1 extends Activity {
	
	private static final String URI = "content://com.example.providers.personprovider";
	private ArrayList<Person> list;
	private PersonService ps;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ps = new PersonService(this);
//		ps.transactionOperation(17, 16);
		list = (ArrayList<Person>)ps.qurey(PersonService.NO_VALUE);
		
		ListView listView = (ListView)this.findViewById(R.id.listView);
		Button insert = (Button) this.findViewById(R.id.button);
		Button delete = (Button) this.findViewById(R.id.button1);
		Button update = (Button) this.findViewById(R.id.button2);
		Button qurey = (Button) this.findViewById(R.id.button3);
		final ContentResolver contentResolver = getContentResolver();      
		
		final PersonAdapter adapter = new PersonAdapter(this);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView lView = (ListView)parent;
				Person person = (Person)lView.getItemAtPosition(position);
				
				int in = person.getPersonId();
				ps.transactionOperation(in, in + 1);
				
				list = (ArrayList<Person>) ps.qurey(PersonService.NO_VALUE);
				adapter.notifyDataSetChanged();
			}
		});
		
		insert.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse(URI + "/person");
				ContentValues contentValues = new ContentValues();
				contentValues.put("name", "拉风");
				contentValues.put("amount", 3500);
				contentResolver.insert(uri, contentValues);
			}
		});
		
		
		delete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse(URI + "/person/20");
				contentResolver.delete(uri, null, null);
			}
		});
		
		update.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse(URI + "/person/20");
				ContentValues values = new ContentValues();
				values.put("name", "lifeng");
				contentResolver.update(uri, values, null, null);
				
			}
		});
		
		qurey.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse(URI + "/person");
				Cursor cursor = contentResolver.query(uri, null, null, null, null);
				list.clear();
				
				while (cursor.moveToNext()) {
					list.add(new Person().
							setPersonId(cursor.getInt(cursor.getColumnIndex("_id"))).
							setName(cursor.getString(cursor.getColumnIndex("name"))).
							setAmount(cursor.getLong(cursor.getColumnIndex("amount"))));
				}
				
				adapter.notifyDataSetChanged();
				
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	private class PersonAdapter extends BaseAdapter {

		private Context context;
		private LayoutInflater mInflater;
		
		public PersonAdapter(Context context) {
			this.context = context;
	        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		} 
		
		@Override
		public int getCount() {   
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Person person = (Person)list.get(position);
			TextView id;
			TextView name;
			TextView amount;
			
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item, parent, false);
			}
			
			id = (TextView)convertView.findViewById(R.id.personid);
			id.setText(Integer.toString(person.getPersonId()));
			
			name = (TextView)convertView.findViewById(R.id.name);
			name.setText(person.getName());
			
			amount = (TextView)convertView.findViewById(R.id.amount);
			amount.setText(Long.toString(person.getAmount()));
			
			return convertView;
		}
	} 
	
	

}
